package Project1;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Query;
import javax.persistence.Table;

import java.io.Serializable;
import java.sql.Date; 

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

import java.util.List;


@Entity
@Table(name="salaries")
public class Salaries implements Serializable
{
	private long emp_no;
	private long salary;
	private Date from_date;
	private Date to_date;
	private Employee employee;
	
	public Salaries() {};
	public Salaries(long num){
		this.emp_no = num;
	}
	
	//*******************GETTTERS SETTERS*************************************
	@Id
	@Column(name ="emp_no")
	public long getEmp_no(){return emp_no;}
	public void setEmp_no(long num){this.emp_no=num;}
	
	@Column(name= "salary")
	public long getSalary() {return salary;}
	public void setSalary(long salary){this.salary = salary;}
	
	@Id
	@Column(name = "from_date")
		public Date getFrom_date() {return from_date; }
		public void setFrom_date(Date from_date) {this.from_date = from_date;}
	
	@Column(name = "to_date")
	 
		public Date getTo_date() {return to_date;}
		public void setTo_date(Date date){this.to_date = date;}
	
	@ManyToOne
	@JoinColumn(name="emp_no")
		public Employee getEmployee() {return employee;}
		public void setEmployee(Employee employee) {this.employee = employee;}
  
	//**************METHODS*************************************
	public void print(){
	  System.out.printf("%d: %d %s %s\n", emp_no, salary, from_date.toString(),to_date.toString()); 
	} 
	//******************STATIC METHODS****************************	
  public static void list(){
	  Session session = HibernateContext.getSession();
	  Criteria criteria= session.createCriteria(Salaries.class);
	  criteria.addOrder(Order.asc("from_date"));
		
		
	  List<Salaries> salary = criteria.list();
	  System.out.println("All Salaries: ");
	  for(Salaries s : salary){
		s.print();
	  }
	  session.close();
  }

}
